library(plotly)

# 1. 模拟数据
set.seed(123)
x <- rnorm(40, mean = 5, sd = 2.2)

# 2. 构建参数网格
mu_vals <- seq(2, 8, length.out = 200)
sigma_vals <- seq(0.5, 4, length.out = 200)

log_likelihood <- matrix(NA, nrow = length(mu_vals), ncol = length(sigma_vals))

# 3. 计算 log-likelihood
for (i in 1:length(mu_vals)) {
  for (j in 1:length(sigma_vals)) {
    mu <- mu_vals[i]
    sigma <- sigma_vals[j]
    log_likelihood[i, j] <- prod(dnorm(x, mean = mu, sd = sigma))
  }
}

# 4. 绘图
plot_ly(
  x = mu_vals,
  y = sigma_vals,
  z = log_likelihood,
  type = "surface"
) %>%
  layout(
    title = "Log-Likelihood Surface of Normal Distribution",
    scene = list(
      xaxis = list(title = "mu"),
      yaxis = list(title = "sigma"),
      zaxis = list(title = "log-likelihood")
    )
  )
